#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int fd;

void handler(int sig)
{
    write(fd, "[ALARM] Timer expired!\n", 22);
}

void mysleep(int sec)
{
    signal(SIGALRM, handler);
    alarm(sec);
    pause(); // 阻塞等待信号
}

int main()
{
    fd = open("mysleep.log", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    write(fd, "=== Custom Sleep Test ===\n", 26);

    write(fd, "[START] Before sleep\n", 21);
    mysleep(3);
    write(fd, "[END] After sleep\n", 18);

    close(fd);
    return 0;
}